package com.eascs.admin.o2o.generation.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.utils.StringUtils;
import com.eascs.admin.o2o.badloan.service.O2oBadLoanService;
import com.eascs.admin.o2o.generation.entity.O2oGenerationEntity;
import com.eascs.admin.o2o.generation.service.O2oGenerationService;
import com.eascs.admin.o2o.generation.vo.O2oGenerationVo;
import com.eascs.admin.o2o.internal.util.BaseUtils;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.entity.O2oPlatformRegional;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.datalist.entity.O2oDataListEntity;
import com.eascs.file.FileService;
import com.eascs.file.FileTypeConstants;
import com.eascs.loan.form.O2oPaymentsForm;
import com.eascs.loan.service.O2oLoanService;
import com.eascs.loan.service.O2oPaymentsService;
import com.eascs.loan.vo.O2oRepaymentRecordVo;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxConfirmButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.entity.sequence.SequenceEntity;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;
import com.eascs.workflow.common.service.sequence.SequenceService;
import com.eascs.workflow.common.service.wf.WorkFlowService;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.WFRequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.Format;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/rgeneration")
public class O2oGenerationRestController extends BaseRestController {
	private final static String PROECESS_TYPE_DHK = "DHK";
	private static Logger log = LoggerFactory.getLogger(O2oGenerationRestController.class);

	@Autowired
	O2oGenerationService generationService;
	@Autowired
	O2oLoanapplyService loanService;
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	@Autowired
	O2oLoanService o2oLoanService;
	@Autowired
	private O2oBadLoanService service;

	/**
	 * @Description:
	 * 				<p>
	 *               显示添加页面
	 *               </p>
	 * @author CXW
	 * @date 2017年5月17日 下午5:07:46
	 * @version V1.0
	 */
	@RequestMapping("/add_view")
	public BaseRestResult<String> addView(O2oGenerationVo vo) {

		return HtmlRestResult.popHtmlFragment("generation/add_view");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               显示添加页面
	 *               </p>
	 * @author CXW
	 * @date 2017年5月17日 下午5:07:46
	 * @version V1.0
	 */
	@RequestMapping("/edit_view")
	public BaseRestResult<String> editView(O2oGenerationVo vo) {
		O2oGenerationEntity entity = generationService.getById(vo.getId());
		ViewContext.put("vo", entity);
		return HtmlRestResult.popHtmlFragment("generation/edit_view");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               保存
	 *               </p>
	 * @author CXW
	 * @date 2017年5月17日 下午5:07:30
	 * @version V1.0
	 */
	@RequestMapping("/save")
	public BaseRestResult<String> save(O2oGenerationEntity entity, String type) {
		if (StringUtils.isEmpty(entity.getLoanBillCode())) {
			return BaseRestResult.warn("请填写贷款单号");
		}
		O2oLoanapplyEntity loanapplyEntity = loanService.findById(entity.getLoanApplyNo());
		int i = insert(entity, loanapplyEntity);
		if (i > 0) {
			if (type.equals("SAS")) {
				ModelResult<Boolean> modelResult = startPayBack(entity, loanapplyEntity);
				if (modelResult.isSuccess()) {
					entity.setFlowStatus(StatusContexts.O2O_BANK_RUN);
					entity.setFlowDtartTime(new Date());
					int j = generationService.update(entity);
					if (j > 0)
						return BaseRestResult.info("保存成功并启动成功").andRefresh();
				} else {
					log.error("发起还款流程失败，失败原因：{}", modelResult.getDetailStack());
					return BaseRestResult.info("保存成功," + modelResult.getErrMsg()).andRefresh();
				}
			}
			return BaseRestResult.info("保存成功").andRefresh();
		}
		return BaseRestResult.warn("保存失败");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               保存方法
	 *               </p>
	 * @author CXW
	 * @date 2017年5月17日 下午5:07:15
	 * @version V1.0
	 */
	public int insert(O2oGenerationEntity entity, O2oLoanapplyEntity loanapplyEntity) {
		User user = UserHolder.getUser();
		entity.setBillNum(buildCode(PROECESS_TYPE_DHK));
		entity.setCreateTime(new Date());
		entity.setCreateUserId(user.getNo());
		entity.setCreateUserName(user.getCname());
		entity.setIsDellete(StatusContexts.O2O_NO);
		// 查询贷款信息和贷款用户信息

		entity.setLoanUserNo(loanapplyEntity.getLoanId());
		entity.setOperId(loanapplyEntity.getOperatorNo());
		entity.setOperName(loanapplyEntity.getOperatorName());
		// 省区id \name
		O2oPlatformRegional o2oPlatformRegional = generationService.findLtd(loanapplyEntity.getLtd());
		entity.setLtdId(loanapplyEntity.getLtd());
		entity.setLtdCode(loanapplyEntity.getLtdCode());
		entity.setLtdName(loanapplyEntity.getLtdName());
		entity.setProvinceCode(o2oPlatformRegional.getProvinceCode());
		entity.setProvinceName(o2oPlatformRegional.getProvinceName());
		entity.setFlowStatus(StatusContexts.O2O_NEW);
		int i = generationService.insert(entity);
		return i;
	}

	/**
	 * @Description:
	 * 				<p>
	 *               更新信息
	 *               </p>
	 * @author CXW
	 * @date 2017年5月17日 下午5:07:15
	 * @version V1.0
	 */
	@RequestMapping("/edit_save")
	public BaseRestResult<String> edit(O2oGenerationEntity entity, String type) {
		if (StringUtils.isEmpty(entity.getLoanBillCode())) {
			return BaseRestResult.warn("请填写贷款单号");
		}
		O2oGenerationEntity generationEntity = generationService.getById(entity.getId());
		// 查询贷款信息和贷款用户信息
		generationEntity.setLoanBillCode(entity.getLoanBillCode());
		generationEntity.setLoanApplyNo(entity.getLoanApplyNo());
		generationEntity.setLoanUserName(entity.getLoanUserName());
		generationEntity.setLoanTime(entity.getLoanTime());
		generationEntity.setLoanTerm(entity.getLoanTerm());
		int i = generationService.update(generationEntity);
		if (i > 0) {
			if (type.equals("SAS")) {
				O2oLoanapplyEntity loanapplyEntity = loanService.findById(generationEntity.getLoanApplyNo());
				ModelResult<Boolean> modelResult = startPayBack(entity, loanapplyEntity);
				if (modelResult.isSuccess()) {
					entity.setFlowStatus(StatusContexts.O2O_BANK_RUN);
					entity.setFlowDtartTime(new Date());
					int j = generationService.update(entity);
					if (j > 0)
						return BaseRestResult.info("保存成功并启动成功").andRefresh();
				} else {
					log.error("发起还款流程失败，失败原因：{}", modelResult.getDetailStack());
					return BaseRestResult.info("保存成功," + modelResult.getErrMsg()).andRefresh();
				}
			}
			return BaseRestResult.info("保存成功").andRefresh();
		}
		return BaseRestResult.warn("保存失败");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               保存并启动
	 *               </p>
	 * @author CXW
	 * @date 2017年5月17日 下午5:06:57
	 * @version V1.0
	 */
	@RequestMapping("/startAndSave")
	public BaseRestResult<String> startAndSave(O2oGenerationEntity entity) {
		log.info("保存并启动开始访问，实体类：{}", entity);
		if (StringUtils.isEmpty(entity.getLoanBillCode())) {
			return BaseRestResult.warn("请填写贷款单号");
		}
		O2oLoanapplyEntity loanapplyEntity = loanService.findById(entity.getLoanApplyNo());
		int i = insert(entity, loanapplyEntity);
		if (i > 0) {
			ModelResult<Boolean> modelResult = startPayBack(entity, loanapplyEntity);
			if (modelResult.isSuccess()) {
				entity.setFlowStatus(StatusContexts.O2O_BANK_RUN);
				entity.setFlowDtartTime(new Date());
				int j = generationService.update(entity);
				if (j > 0)
					return BaseRestResult.info("保存成功并启动成功").andRefresh();
			} else {
				log.error("发起还款流程失败，失败原因：{}", modelResult.getDetailStack());
				return BaseRestResult.info("保存成功," + modelResult.getErrMsg()).andRefresh();
			}
		}
		return BaseRestResult.warn("保存失败");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               启动
	 *               </p>
	 * @author CXW
	 * @date 2017年5月17日 下午5:06:57
	 * @version V1.0
	 */
	@RequestMapping("/start")
	public BaseRestResult<String> start(O2oGenerationVo entity) {
		O2oGenerationEntity generationEntity = generationService.getById(entity.getId());
		O2oLoanapplyEntity loanapplyEntity = loanService.findById(generationEntity.getLoanApplyNo());
		ModelResult<Boolean> modelResult = startPayBack(generationEntity, loanapplyEntity);
		if (modelResult.isSuccess()) {
			generationEntity.setFlowStatus(StatusContexts.O2O_BANK_RUN);
			generationEntity.setFlowDtartTime(new Date());
			generationService.update(generationEntity);
			return BaseRestResult.info("启动成功").andRefresh();
		} else {
			log.error("发起还款流程失败，失败原因：{},{}", modelResult.getDetailStack(), modelResult.getErrMsg());
			return BaseRestResult.info(modelResult.getErrMsg()).andRefresh();
		}
	}

	@Value("${o2o.admin.newGenerationUrl}")
	String startUrl;
	@Reference
	private WorkFlowService workFlowService;

	/**
	 * 启动回款流程
	 */
	public ModelResult<Boolean> startPayBack(O2oGenerationEntity entity, O2oLoanapplyEntity loan) { // 根据单号去启动流程
		ModelResult<Boolean> modelResult = new ModelResult<>();
		if (null == loan) {
			return modelResult.withError("error billCode O2O SQL not find", "贷款单号异常，请检查数据是否正常");
		}
		// 获取 o2opay对象
		WFRequestParam param = new WFRequestParam();
		param.setReferId(entity.getId());
		param.setReferCode(loan.getBillCode());
		// 回款流程的url
		param.setReferUrl(startUrl + "?billCode=" + loan.getBillCode());
		param.setSystemModule(ProcessTemplate.SYSTEM_MODULE_O2O);
		param.setProcessType(ProcessTemplate.PROECESS_TYPE_DHK); // 代还款确认流程编号
		User user = UserHolder.getUser();
		param.setAppliedby(user.getNo());
		param.setAppliedbyName(user.getCname());
		param.setOrgNo(loan.getLtd());
		StringBuffer appliedDes = new StringBuffer("借款人:");
		appliedDes.append(o2oLoanapplyService.findLoanUserEntity(loan.getLoanapplyNo(),loan.getLoanId()).getUserFullName());
		appliedDes.append("，放款金额:");
		java.text.DecimalFormat df         =new java.text.DecimalFormat("0.00"); //格式化，保留两位小数
		BigDecimal loanAmount=new BigDecimal(0);
		if ("EA".equals(loan.getLoanType())||"FQ".equals(loan.getLoanType())) {
			loanAmount = service.findDataByBillCode(loan.getBillCode()).getLoanAmount();
		}else{
			loanAmount = o2oLoanService.queryBillCode(loan.getBillCode()).getLoanAmount();
		}
		appliedDes.append(df.format(loanAmount));
		param.setAppliedDes(appliedDes.toString());
		log.info("启动回款流程，参数有贷款id：【{}】，贷款编号：【{}】，系统模块：【{}】，流程类型：【{}】，经营平台id：【{}】，贷款摘要信息：【{}】", entity.getId(), loan.getBillCode(), ProcessTemplate.SYSTEM_MODULE_O2O, param.getProcessType(), loan.getLtd(), appliedDes.toString());
		modelResult = workFlowService.instance(param);
		return modelResult;
	}

	@RequestMapping("/searchByBillCode")
	public BaseRestResult<List<Object[]>> searchByBillCode(String code) {
		List<Object[]> objsList = new ArrayList<>();
		if (!StringUtils.isEmpty(code)) {
			objsList = generationService.finds(code);
		}
		return BaseRestResult.buildRestResult(objsList);
	}
	
	
	@RequestMapping("/searchByBillCodecommon")
	public BaseRestResult<List<Object[]>> searchByBillCodecommon(String code) {
		List<Object[]> objsList = new ArrayList<>();
		if (!StringUtils.isEmpty(code)) {
			objsList = generationService.findscommon(code);
		}
		return BaseRestResult.buildRestResult(objsList);
	}
	

	@Reference
	private SequenceService sequenceService;

	/**
	 * 生成贷款编号
	 * 
	 * @param code
	 * @return
	 */
	public String buildCode(String code) {
		String bill = StringUtils.getFourBitYearMonth(new Date());
		String prefix = code + "-" + bill;
		String flag = sequenceService.getSeq(SequenceEntity.O2O_LOAN_NO, StatusContexts.O2O_SEQUENCE_LENGTH, prefix);
		if (flag == null) {
			throw new RuntimeException();
		}
		return flag;
	}

	@Autowired
	private BaseUtils baseUtils;

	/**
	 * @Description:
	 * 				<p>
	 *               代办详情
	 *               </p>
	 * @author CXW
	 * @date 2017年5月18日 上午9:35:42
	 * @version V1.0
	 */
	@RequestMapping("/approl")
	public BaseRestResult<String> approl(O2oRepaymentRecordVo entity) {
		log.info("工作流访问代还款详情：参数billCode：【{}】，referId:【{}】", entity.getBillCode(), entity.getReferId());
		O2oGenerationEntity generationEntity = generationService.getById(entity.getReferId());
		log.info("查询代还款信息：【{}】", generationEntity);
		String approvalUrl = baseUtils.getApprovalUrl(entity.getNodeAuditorId(), ProcessTemplate.SYSTEM_MODULE_O2O);// 审批代办列表详情URL
		ViewContext.put("approvalUrl", approvalUrl);
		String agreeUrl = baseUtils.getApprovalAgreeUrl(entity.getReferId(), entity.getNodeAuditorId(), ProcessTemplate.SYSTEM_MODULE_O2O);
		ViewContext.put("agreeUrl", agreeUrl);
		ViewContext.put("referId", entity.getReferId());
		ViewContext.put("approvalStatus", entity.getApprovalStatus());
		ViewContext.put("generationEntity", generationEntity);
		return HtmlRestResult.popHtmlFragment("generation/approl");
	}

	@Autowired
	O2oPaymentsService o2oPaymentsService;

	/**
	 * @Description:
	 * 				<p>
	 *               还款计划公共部分
	 *               </p>
	 * @author CXW
	 * @date 2017年5月18日 上午10:28:27
	 * @version V1.0
	 */
	@RequestMapping("/plan_common")
	public PaginationRestResult planCommon(String billCode) {
		O2oPaymentsForm o2oPaymentsForm = new O2oPaymentsForm();
		o2oPaymentsForm.setBillCode(billCode);
		o2oPaymentsForm.setPageSize(100);
		PageResult<Object[]> pageResult = o2oPaymentsService.paymentsList(o2oPaymentsForm);
		String[] titles = { "贷款单号", "借款人姓名", "借款人证件号", "还款期数", "应还款日期", "剩余本金", "应还本金", "应还利息" };
		String pageUrl = "/rgeneration/plan_common";
		pageUrl = pageUrl + "?showPage=";
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, pageUrl);
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setFormats(new Format[] { null, null, null, null, null, new DecimalFormat(",##0.00"), new DecimalFormat(",##0.00"), new DecimalFormat(",##0.00") });
		paginationInfo.setRendTo("#result_list");
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

	/**
	 * @Description:
	 * 				<p>
	 *               显示还款计划
	 *               </p>
	 * @author CXW
	 * @date 2017年5月18日 上午10:35:25
	 * @version V1.0
	 */
	@RequestMapping("/plan_show")
	public BaseRestResult<String> planCommonView(String billCode) {
		log.info("查询还款计划billcode:【{}】", billCode);
		ViewContext.put("billCode", billCode);
		return new HtmlRestResult("generation/plan_common");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               详情
	 *               </p>
	 * @author CXW
	 * @date 2017年5月18日 上午9:35:42
	 * @version V1.0
	 */
	@RequestMapping("/approl_detail")
	public BaseRestResult<String> approlDetail(String id) {
		log.info("详情referId:【{}】", id);
		O2oGenerationEntity generationEntity = generationService.getById(id);
		String agreeUrl = baseUtils.getApprovalAgreeUrl(id, null, ProcessTemplate.SYSTEM_MODULE_O2O);
		ViewContext.put("agreeUrl", agreeUrl);
		ViewContext.put("referId", id);
		ViewContext.put("generationEntity", generationEntity);
		return HtmlRestResult.popHtmlFragment("generation/approl_detail");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               动态表单上传文件
	 *               </p>
	 * @author CXW
	 * @date 2017年5月18日 上午10:54:27
	 * @version V1.0
	 */
	@RequestMapping("/upload_file")
	public BaseRestResult<String> uploadFile(String recordId, String isupload, String fileTyle) {
		PageResult<Object[]> pageResult = loanService.findO2oDataListEntityById(recordId, "'" + fileTyle + "'");
		String titles[] = { "附件类型", "文件名", "提交人", "提交时间", "更新时间" };
		Button[] titleButtons = new Button[] { null, ButtonHelper.createOpenButton(null, "/file/download?datalistno=") };
		AjaxConfirmButton[] dealButton = null;
		if (!StringUtils.isEmpty(isupload)) {
			dealButton = new AjaxConfirmButton[] { new AjaxConfirmButton("删除 ", "/loanrest/delete/") };
		}
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles);
		paginationInfo.setMoreButtons(dealButton);
		paginationInfo.setTitleButtons(titleButtons);
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setBuildRowNumber(true);
		ViewContext.put("paginationInfo", paginationInfo);// 个人附件
		ViewContext.put("recordId", recordId);
		ViewContext.put("isupload", isupload);
		return new HtmlRestResult("generation/upload_file");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               信贷经理动态表单
	 *               </p>
	 * @author CXW
	 * @date 2017年5月18日 上午11:24:25
	 * @version V1.0
	 */
	@RequestMapping("/add_one_form")
	public BaseRestResult<String> oneForm(String referId) {
		O2oGenerationEntity generationEntity = generationService.getById(referId);
		ViewContext.put("isupload", "UPLOAD");
		ViewContext.put("vo", generationEntity);
		return new HtmlRestResult("generation/add_one_form");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               财务动态表单
	 *               </p>
	 * @author CXW
	 * @date 2017年5月18日 上午11:24:25
	 * @version V1.0
	 */
	@RequestMapping("/add_tow_form")
	public BaseRestResult<String> towForm(String referId) {
		O2oGenerationEntity generationEntity = generationService.getById(referId);
		ViewContext.put("isupload", "");
		ViewContext.put("vo", generationEntity);
		return new HtmlRestResult("generation/add_one_form");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               详情代还款信息
	 *               </p>
	 * @author CXW
	 * @date 2017年5月18日 上午11:24:25
	 * @version V1.0
	 */
	@RequestMapping("/detail_form")
	public BaseRestResult<String> detailForm(String referId) {
		O2oGenerationEntity generationEntity = generationService.getById(referId);
		ViewContext.put("isupload", "");
		ViewContext.put("vo", generationEntity);
		return new HtmlRestResult("generation/detail_form");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               保存动态表单信息
	 *               </p>
	 * @author CXW
	 * @date 2017年5月18日 下午1:47:09
	 * @version V1.0
	 */
	@RequestMapping("/update")
	public BaseRestResult<String> update(O2oGenerationEntity entity) {
		if (StringUtils.isEmpty(entity.getPlayUsername())) {
			return BaseRestResult.warn("请填写实际打款人姓名");
		}
		if (entity.getPlayMoney() == null) {
			return BaseRestResult.warn("请填写实际打款金额");
		}
		if (entity.getPlayTime() == null) {
			return BaseRestResult.warn("请填写打款时间");
		}
		if (isUploadFile(entity.getId(), FileTypeConstants.DALIST_TYPE_DHK)) {
			O2oGenerationEntity generationEntity = generationService.findById(entity.getId());
			generationEntity.setPlayMoney(entity.getPlayMoney());
			generationEntity.setPlayTime(entity.getPlayTime());
			generationEntity.setPlayUsername(entity.getPlayUsername());
			int i = generationService.update(generationEntity);
			if (i > 0) {
				return BaseRestResult.info("保存成功");
			} else {
				return BaseRestResult.warn("保存失败");
			}
		} else
			return BaseRestResult.warn("请先上传文件");
	}

	@Autowired
	private FileService fileService;

	public boolean isUploadFile(String loanapplyNo, String type) {
		O2oDataListEntity data = fileService.getByIdAndByType(loanapplyNo, type);
		if (null == data) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * @Description:
	 * 				<p>
	 *               代还款详情按钮
	 *               </p>
	 * @author CXW
	 * @date 2017年5月18日 下午3:14:16
	 * @version V1.0
	 */
	@RequestMapping("/buttons")
	public BaseRestResult<String> bottons(O2oGenerationVo vo) {
		User user = UserHolder.getUser();
		O2oGenerationEntity generationEntity = generationService.findById(vo.getId());
		List<Button> bottons = new ArrayList<>();
		if (generationEntity != null) {
			if (!StringUtils.isEmpty(generationEntity.getFlowStatus())) {
				if (generationEntity.getFlowStatus().equals(StatusContexts.O2O_NEW)) {
					if (user.getNo().equals(generationEntity.getCreateUserId())) {
						bottons.add(ButtonHelper.createAjaxConfirmButton("删除本条数据", "/rgeneration/delete?id=" + vo.getId()));
						bottons.add(ButtonHelper.createAjaxButton("编辑", "/rgeneration/edit_view?id=" + vo.getId()));
						bottons.add(ButtonHelper.createAjaxButton("启动代还款流程", "/rgeneration/start?id=" + vo.getId()));
					} else {
						bottons.add(ButtonHelper.createAjaxConfirmButton("删除本条数据", "/rgeneration/delete?id=" + vo.getId(), "generationDelete"));
						bottons.add(ButtonHelper.createAjaxButton("编辑", "/rgeneration/edit_view?id=" + vo.getId(), "generationEdit"));
						bottons.add(ButtonHelper.createAjaxButton("启动代还款流程", "/rgeneration/start?id=" + vo.getId(), "generationStart"));
					}
				}
			}
		}
		ViewContext.put("buttons", bottons);
		ViewContext.put("entity", generationEntity);
		return new HtmlRestResult("generation/detail_button");
	}

	/**
	 * @Description:
	 * 				<p>
	 *               删除
	 *               </p>
	 * @author CXW
	 * @date 2017年5月18日 下午3:18:41
	 * @version V1.0
	 */
	@RequestMapping("/delete")
	public BaseRestResult<String> delete(O2oGenerationVo vo) {
		O2oGenerationEntity generationEntity = generationService.getById(vo.getId());
		generationEntity.setIsDellete(StatusContexts.O2O_YES);
		int i = generationService.update(generationEntity);
		if (i > 0) {
			return BaseRestResult.info("删除成功").andRefresh();
		}
		return BaseRestResult.warn("删除失败");
	}
}
